//
//  ZYCCompleteBasicInformationViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/15.
//

#import "ZYCCompleteBasicInformationViewController.h"
#import "ZYCCompleteBasicInformationCell.h"
#import "ZYCProfileUserInfoFooterView.h"
#import "ZYCCommonSelectObjectPickerView.h"
#import "ZYCCommonBaseModel.h"
#import "NSString+File.h"

@interface ZYCCompleteBasicInformationViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCProfileUserInfoFooterViewDelegate,ZYCCommonSelectObjectPickerViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSMutableArray *sexArray;

/** 学籍号*/
@property (nonatomic, strong) NSString *stuNum;
/** 姓名*/
@property (nonatomic, strong) NSString *name;
/** 曾用名*/
@property (nonatomic, strong) NSString *oldName;
/** 年级*/
@property (nonatomic, strong) NSString *gradeName;
/** 班级*/
@property (nonatomic, strong) NSString *className;
/** 性别*/
@property (nonatomic, strong) NSString *sexy;
/** 性别*/
@property (nonatomic, strong) NSString *sexValue;
/** 性别*/
@property (nonatomic, strong) NSString *idCardNo;


@property (nonatomic, strong) ZYCCommonSelectObjectPickerView *sexPickerView;



@end

@implementation ZYCCompleteBasicInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"基本信息";
    [self.view addSubview:self.tableView];
    [self loadStuBasicInfo];
}

- (void)loadStuBasicInfo{
    NSDictionary *params = @{
        @"id":self.studentId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_POST_CompleteBasicInfo requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSDictionary *dic = responseObject[@"body"];
                self.stuNum = dic[@"num"];
                self.name = dic[@"name"];
                self.oldName = dic[@"oldName"];
                self.gradeName = dic[@"gradeName"];
                self.className = dic[@"className"];
                self.sexValue = dic[@"sexValue"];
                if ([self.sexValue integerValue] == 1) {
                    self.sexy = @"男";
                }else{
                    self.sexy = @"女";
                }
                self.idCardNo = dic[@"idCardNo"];
                [self.tableView reloadData];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCWeakSelf
    ZYCCompleteBasicInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCCompleteBasicInformationCell class]) forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.section];
    cell.titleString = dict[@"title"];
    cell.placeHolderString = dict[@"placeHolder"];
    cell.textChangedBlock = ^(NSString * _Nonnull titleString, NSString * _Nonnull text) {
        [weakSelf
         cellWithWithTitleString:titleString textChanged:text];
    };
    switch (indexPath.section) {
        case 0:{
            cell.contentString = self.stuNum;
            break;
        }
        case 1:{
            cell.contentString = self.name;
            break;
        }
        case 2:{
            cell.contentString = self.oldName;
            break;
        }
        case 3:{
            cell.contentString = self.gradeName;
            break;
        }
        case 4:{
            cell.contentString = self.className;
            break;
        }
        case 5:{
            cell.contentString = self.sexy;
            break;
        }
        case 6:{
            cell.contentString = self.idCardNo;
            break;
        }
        default:
            break;
    }
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 6.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 6) {
        return 200.0f;
    }
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    // 完善更多资料
    if (section == 6) {
        ZYCProfileUserInfoFooterView *footerView = [[ZYCProfileUserInfoFooterView alloc] init];
        footerView.titleString = @"提交";
        footerView.delegate = self;
        return footerView;;
    }
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 5) {
        [self.sexPickerView show];
    }
}

- (void)cellWithWithTitleString:(NSString *)titleString textChanged:(NSString *)text{
  
    if ([titleString isEqualToString:@"学籍号/学号:"]) {
        self.stuNum = text;
    }else if ([titleString isEqualToString:@"*学生姓名:"]){
        self.name = text;
    }else if ([titleString isEqualToString:@"曾用名:"]){
        self.oldName = text;
    }else if ([titleString isEqualToString:@"*身份证号:"]){
        self.idCardNo = text;
    }
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[
            @{@"title":@"学籍号/学号:",@"placeHolder":@"请输入学籍号/学号"},
            @{@"title":@"*学生姓名:",@"placeHolder":@"请输入学生姓名"},
            @{@"title":@"曾用名:",@"placeHolder":@"请输入曾用名"},
            @{@"title":@"*所在年级:",@"placeHolder":@""},
            @{@"title":@"*所在班级:",@"placeHolder":@""},
            @{@"title":@"*性别:",@"placeHolder":@""},
            @{@"title":@"*身份证号:",@"placeHolder":@"请输入身份证号码"}
        ];
    }
    return _dataSource;
}

#pragma mark - ZYCCommonSelectObjectPickerViewDelegate
- (void)selectObjectPickerView:(ZYCCommonSelectObjectPickerView *)pickerView DidClickModel:(ZYCCommonBaseModel *)model{
    if (pickerView == self.sexPickerView) {
        self.sexy = model.name;
        self.sexValue = model.nameId;
        NSIndexSet *indexSet= [[NSIndexSet alloc]initWithIndex:5];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    }
    
}
#pragma mark - ZYCProfileUserInfoFooterViewDelegate
- (void)profileUserInfoFooterViewDidMore{
    if (self.name.length == 0) {
        [MrjToast makeToast:@"请输入姓名" makeView:self.view];
        return;;
    }
    
    if ([self.stuNum includeChinese]) {
        [MrjToast makeToast:@"学籍号/学号不能包含汉字" makeView:self.view];
        return;
    }
    
    if (self.idCardNo.length == 0) {
        [MrjToast makeToast:@"请输入身份证号" makeView:self.view];
        return;;
    }
    
    if (![NSString checkIdentityCardNo:self.idCardNo]) {
        [MrjToast makeToast:@"身份证号有误" makeView:self.view];
        return;;
    }
    if ([NSString stringContainsEmoji:self.stuNum] || [NSString stringContainsEmoji:self.name]||[NSString stringContainsEmoji:self.oldName]||[NSString stringContainsEmoji:self.idCardNo]) {
        [MrjToast makeToast:@"不能输入表情" makeView:self.view];
        return;
    }
    
    NSDictionary *params = @{
        @"id":self.studentId,
        @"num":self.stuNum ? self.stuNum : @"",
        @"name":self.name,
        @"oldName":self.oldName ? self.oldName : @"",
        @"sexValue":self.sexValue,
        @"idCardNo":self.idCardNo
    };
    [MrjSVProgressHUD showWithStatus:@"修改中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_POST_UpdateStudentByParent requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:@"修改成功" makeView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];

}
#pragma mark - getters

- (ZYCCommonSelectObjectPickerView *)sexPickerView{
    if (!_sexPickerView) {
        _sexPickerView = [[ZYCCommonSelectObjectPickerView alloc]init];
        _sexPickerView.tipString = @"请选择性别";
        _sexPickerView.delegate = self;
        _sexPickerView.dataSource = self.sexArray;
        [self.view addSubview:_sexPickerView];
    }
    return _sexPickerView;
}


- (NSMutableArray *)sexArray {
    if (!_sexArray) {
        NSMutableArray *temp = [NSMutableArray array];
        {
            ZYCCommonBaseModel *model = [[ZYCCommonBaseModel alloc] init];
            model.name = @"男";
            model.nameId = @"1";
            [temp addObject:model];
        }
        {
            ZYCCommonBaseModel *model = [[ZYCCommonBaseModel alloc] init];
            model.name = @"女";
            model.nameId = @"2";
            [temp addObject:model];
        }
        _sexArray = temp;
    }
    return _sexArray;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HEX_RGB(@"#FAFAFA");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCCompleteBasicInformationCell class] forCellReuseIdentifier:NSStringFromClass([ZYCCompleteBasicInformationCell class])];
        
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
